/*
 * 文件名：AdInfoController.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：lilan
 * 修改时间：2016年4月20日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.seller.web.adinfo;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.AdCategory;
import com.zcsy.manage.model.mall.AdInfo;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.mall.IAdCategoryService;
import com.zcsy.manage.service.mall.IAdInfoService;
import com.zcsy.manage.service.mall.IGoodsInfoService;

/**
 * 
 * @Title: AdInfoController.java @Package： com.zcsy.seller.web.adinfo
 * @Description: 广告信息控制层
 *
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年6月6日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "ad")
public class AdInfoController {

    @Autowired
    private IAdInfoService adInfoService;

    @Autowired
    private IAdCategoryService adCategoryService;

    @Autowired
    private IGoodsInfoService goodsInfoService;
    @Autowired
    private IShopInfoService shopInfoService;

    private static final int PAGE_SIZE = 5; // 每页大小
    private static final int PAGE_NO = 1; // 默认首页
    /** 新申请 */
    private static final int STATE_NEW = 1;
    /** 正常 */
    private static final int STATE_SUCCESS = 2;
    /** 禁用 */
    private static final int STAET_FORBIDEN = 5;
    /** 商户 */
    private static final int TYPE_BUSINESS = 2;
    /** 对象类型为店铺 */
    private static final int TYPE_OBJ_SHOP = 1;
    /** 对象类型为商品 */
    private static final int TYPE_OBJ_GOODS = 2;

    private static final int SORT_DEFAULT = 0;

    /**
     * 
     * @Description 返回广告位申请列表页面<br>
     * 
     * @param model
     * @param pNo
     *            当前页
     * @param userAccount
     *            发布账号
     * @param req
     *            用户请求
     * @return
     * @author lilan 1063972142@qq.com
     * @date 2016年4月20日
     * @see
     */
    @RequestMapping(value = "/adApply", method = RequestMethod.GET)
    public String adApply(Model model, String id, HttpServletRequest req) {
        ShopInfo shopInfo = (ShopInfo) req.getSession().getAttribute(RequestConstants.SHOP_ID);
        try {
            if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
                model.addAttribute("type", Constants.SAVE_TYPE);
            } else {
                AdInfo adInfo = adInfoService.selectByPrimaryKey(id);
                if (adInfo != null) {
                    model.addAttribute("type", Constants.EDIT_TYPE);
                    model.addAttribute("adInfo", adInfo);
                    model.addAttribute("adCategoryId", adInfo.getAdCategoryId());
                }
            }
            List<AdCategory> adCategorys = adCategoryService.getAllAdCategory();
            List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoByShopId(shopInfo.getId());
            model.addAttribute("goodsInfos", goodsInfos);
            model.addAttribute("adCategorys", adCategorys);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "adinfo/adApply";
    }

    /**
     * @Description 返回广告位信息列表页面<br>
     * @param model
     * @param pNo
     *            当前页
     * @param title
     *            标题
     * @param adCategoryId
     *            广告位类型id
     * @param req
     *            用户请求
     * @return
     * @author lilan 1063972142@qq.com
     * @date 2016年4月21日
     * @see
     */
    @RequestMapping(value = "/adInfoList", method = RequestMethod.GET)
    public String adInfoList(Model model, Integer pNo, String title, String adCategoryId, HttpServletRequest req) {
//    	ShopInfo shopInfo = (ShopInfo) req.getSession().getAttribute(RequestConstants.SHOP_ID);
        if (pNo == null) {
            pNo = PAGE_NO;
        }
        // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, PAGE_SIZE);

        AdInfo adInfo = new AdInfo();

        if (StringUtils.isNotEmpty(title)) {
            adInfo.setTitle(title);
        }
        
        if (StringUtils.isNotEmpty(adCategoryId)) {
            adInfo.setAdCategoryId(adCategoryId);
        }
//        adInfo.setShopId(shopInfo.getId());
        List<AdInfo> adInfos = adInfoService.selectAdinfoByKeyword(adInfo);
        PageInfo<AdInfo> info = new PageInfo<AdInfo>(adInfos);
        List<AdCategory> adCategorys = adCategoryService.getAllAdCategory();

        model.addAttribute("adCategorys", adCategorys);
        model.addAttribute("adInfo", info);
        model.addAttribute("currentPage", pNo);
        model.addAttribute("title", title);

        return "adinfo/adInfoList";
    }

    /**
     * @Description 返回广告位信息编辑页面<br>
     * @param model
     * @param id
     *            主键ID
     * @author lilan 1063972142@qq.com
     * @date 2016年5月31日
     * @see
     */
    @RequestMapping(value = "editAdInfo", method = RequestMethod.GET)
    public String selectById(Model model, String id, HttpServletRequest req) {
        ShopInfo shopInfo = (ShopInfo) req.getSession().getAttribute(RequestConstants.SHOP_ID);
        try {
            if (String.valueOf(Constants.STATE_DISABLED).equals(id)) {
                model.addAttribute("type", Constants.SAVE_TYPE);
            } else {
                AdInfo adInfo = adInfoService.selectByPrimaryKey(id);
                if (adInfo != null) {
                    model.addAttribute("type", Constants.EDIT_TYPE);
                    model.addAttribute("adInfo", adInfo);
                    model.addAttribute("adCategoryId", adInfo.getAdCategoryId());
                    model.addAttribute("objId", adInfo.getObjId());
                }
            }
            List<AdCategory> adCategorys = adCategoryService.getAllAdCategory();
            List<GoodsInfo> goodsInfos = goodsInfoService.getGoodsInfoByShopId(shopInfo.getId());
            model.addAttribute("goodsInfos", goodsInfos);
            model.addAttribute("adCategorys", adCategorys);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "adinfo/adInfoEdit";
    }

    /**
     * 
     * @Description 更改发布账号状态<br>
     * 
     * @param model
     * @param id
     *            主键id
     * @param state
     *            1新申请 2正常 3禁用 4已拒绝
     * @return
     * @author lilan 1063972142@qq.com
     * @date 2016年4月21日
     * @see
     */
    @RequestMapping(value = "/adApplyUpdate", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson adApplyUpdate(Model model, String id, int state) {
        RequestJson result = new RequestJson();
        int temp = 0;
        try {
            AdInfo adInfo = adInfoService.selectByPrimaryKey(id);
            switch (state) {
            case STATE_SUCCESS:
                temp = STAET_FORBIDEN;
                break;
            case STAET_FORBIDEN:
                temp = STATE_SUCCESS;
                break;
            default:
                temp = STATE_SUCCESS;
                break;
            }
            adInfo.setState(temp);
            adInfoService.update(adInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }

    /**
     * 
     * @Description 保存和更新广告位信息方法<br>
     *              id不为空且不为0，则表示更新 否则表示保存
     * @param title
     *            标题
     * @param id
     *            主键ID
     * @param adCategoryId
     *            广告位类型ID
     * @param beginTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @param img
     *            广告图片
     * @param objType
     *            对象类型 1店铺，2商品，3特殊网页
     * @param webUrl
     *            网页地址
     * @param remark
     *            备注说明
     * @param state
     *            状态
     * @param userAccount
     *            发布账号
     * @param userAccountType
     *            账号类型 1新申请，2正常，3禁用，4审核未通过
     * @param memo
     *            备注
     * @param sort
     *            排序
     * @param req
     *            用户请求
     * @return
     * @author lilan 1063972142@qq.com
     * @date 2016年4月22日
     * @see
     */
    @RequestMapping(value = "saveOrUpdateAdInfo", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveOrUpdateAdInfo(String title, String id, String adCategoryId, String beginTime,
            String endTime, Integer objType, String objId, String webUrl, String remark, String img,
            HttpServletRequest req) {

        RequestJson result = new RequestJson();

        try {
            if (id != "" && !"0".equals(id)) {
                AdInfo adInfo = adInfoService.selectByPrimaryKey(id);

                adInfo.setTitle(title);
                adInfo.setAdCategoryId(adCategoryId);
                adInfo.setBeginTime(beginTime);
                adInfo.setEndTime(endTime);
                adInfo.setObjType(objType);
                adInfo.setSort(SORT_DEFAULT);
                if (objType == TYPE_OBJ_SHOP) {
                    ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(objId);
                    adInfo.setShopLogo(shopInfo.getLogo());
                    adInfo.setShopName(shopInfo.getName());
                } else if (objType == TYPE_OBJ_GOODS) {
                    GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(objId);
                    adInfo.setGoodsLogo(goodsInfo.getLogo());
                    adInfo.setGoodsName(goodsInfo.getName());
                }
                adInfo.setObjId(objId);
                adInfo.setUrl(webUrl);
                adInfo.setRemark(remark);
                adInfo.setImg(img);
                adInfo.setState(STATE_NEW);
                adInfo.setUserAccountType(TYPE_BUSINESS);
                User user = (User) req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
                adInfo.setUserAccount(user.getLoginname());
                // adInfo.setMemo(memo);
                adInfo.setUpdateTime(new Date());
                adInfoService.update(adInfo);
            } else {
                AdInfo adInfo = new AdInfo();
                
                adInfo.setTitle(title);
                adInfo.setAdCategoryId(adCategoryId);
                adInfo.setBeginTime(beginTime);
                adInfo.setEndTime(endTime);
                adInfo.setObjType(objType);
                adInfo.setSort(SORT_DEFAULT);
                if (objType == TYPE_OBJ_SHOP) {
                    ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(objId);
                    adInfo.setShopLogo(shopInfo.getLogo());
                    adInfo.setShopName(shopInfo.getName());
                } else if (objType == TYPE_OBJ_GOODS) {
                    GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(objId);
                    adInfo.setGoodsLogo(goodsInfo.getLogo());
                    adInfo.setGoodsName(goodsInfo.getName());
                }
                adInfo.setObjId(objId);
                adInfo.setUrl(webUrl);
                adInfo.setRemark(remark);
                adInfo.setImg(img);
                adInfo.setState(STATE_NEW);
                adInfo.setUserAccountType(TYPE_BUSINESS);
                User user = (User) req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
                adInfo.setUserAccount(user.getLoginname());
                // adInfo.setMemo(memo);
                adInfo.setId(UniqueNumberUtils.get18UniqueNumber());
                adInfo.setCreateTime(new Date());
                adInfo.setUpdateTime(new Date());
                adInfoService.insert(adInfo);
            }
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("操作失败");
        }

        return result;
    }

    /**
     * 
     * @Description 根据排序实现置顶功能，即修改当前排序为最大排序值加1，并且更新修改时间
     * @param id
     *            主键id
     * @param req
     *            用户请求
     * @return
     * @author lilan 1063972142@qq.com
     * @date 2016年6月14日
     * @see
     */
    @RequestMapping(value = "updateSort", method = RequestMethod.GET)
    @ResponseBody
    public RequestJson updateSort(String id, HttpServletRequest req) {
        RequestJson result = new RequestJson();
        try {
            AdInfo adInfo = adInfoService.selectByPrimaryKey(id);
            if (adInfo != null) {
                // adInfo.setSort(SORT_DEFAULT);
                // adInfoService.update(adInfo);
                adInfo.setUpdateTime(DateUtils.getDate());
                adInfoService.updateSort(adInfo);
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作失败");
            e.printStackTrace();
        }

        return result;
    }

}